#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
高级提示词工程工作流演示
展示如何使用包含A/B测试、自动优化和生产环境测试的高级提示词工程工作流
"""

import asyncio
from src.research_core import (
    create_advanced_prompt_workflow,
    PromptEngineeringState
)

async def demo_advanced_prompt_workflow():
    """演示高级提示词工作流"""
    print("=== 高级提示词工程工作流演示 ===\n")
    
    # 创建工作流
    workflow = create_advanced_prompt_workflow()
    
    # 定义需求
    requirement = """
    我需要一个能够帮助用户进行数据分析的AI助手提示词。这个助手应该能够：
    1. 理解用户的数据分析需求
    2. 根据需求推荐合适的分析方法
    3. 生成相应的Python代码（使用pandas, numpy等库）
    4. 解释分析结果的含义
    5. 提供数据可视化的建议
    6. 注意事项和局限性说明
    """
    
    # 初始化状态
    initial_state = PromptEngineeringState(
        requirement=requirement,
        requirement_analysis="",
        current_prompt=None,
        human_feedback=None,
        feedback_history=[],
        optimization_goal="提高提示词在数据分析任务中的指导性和完整性",
        prompt_evaluation=None,
        final_prompt=None,
        design_reasoning=None,
        iteration_count=0,
        workflow_complete=False,
        human_intervened=False,
        current_stage="start",
        quality_score=0.0,
        execution_time={},
        context_info={},
        metadata={},
        ab_test_results=None,
        performance_metrics=[],
        user_feedbacks=[],
        optimization_history=[],
        prompt_versions={
            "previous": "先前版本的提示词"
        },
        test_results=None,
        user_preferences={},
        domain_context=None,
        interaction_history=[],
        personalization_settings={},
        template_recommendations=[],
        quality_evaluation_details=None,
        ab_test_variants=[],
        quality_history=[],
        decision_log=[],
        workflow_metrics={}
    )
    
    # 执行工作流
    result = workflow.invoke(initial_state)
    
    print("需求分析:")
    print(result.get("requirement_analysis", "无分析结果"))
    print("\n" + "="*50 + "\n")
    
    print("最终生成的提示词:")
    print(result.get("final_prompt", "无提示词生成"))
    print("\n" + "="*50 + "\n")
    
    print("设计说明:")
    print(result.get("design_reasoning", "无设计说明"))
    
    print("\nA/B测试结果:")
    ab_test_results = result.get("ab_test_results", {})
    if ab_test_results:
        for version, metrics in ab_test_results.items():
            print(f"- {version}:")
            for metric, value in metrics.items():
                print(f"  {metric}: {value}")
    
    print("\n优化历史:")
    for optimization in result.get("optimization_history", []):
        print(f"- 时间: {optimization.get('timestamp')}")
        print(f"  原因: {optimization.get('optimization_reason')}")
        print(f"  变更: {optimization.get('changes_made')}")
    
    print("\n用户反馈:")
    for feedback in result.get("user_feedbacks", []):
        print(f"- 类型: {feedback.get('feedback_type')}")
        print(f"  内容: {feedback.get('content')}")
        print(f"  评分: {feedback.get('rating')}")
    
    print("\n生产环境测试结果:")
    test_results = result.get("test_results", {})
    for metric, value in test_results.items():
        print(f"- {metric}: {value}")
    
    print(f"\n最终质量评分: {result.get('quality_score', 0.0)}")

async def main():
    """主函数"""
    await demo_advanced_prompt_workflow()

if __name__ == "__main__":
    asyncio.run(main())